<?php
class helper_users_login
{
	public $errmsg = '';


	//我登陆的时候写userbase对象到session[$_Login_SessionKey]
	static $_Login_SessionKey = 'userbase';

	/**
	 *
	 *2011-12-31-下午12:01:04 by 460932465
	 * @author 成功后读取
	 * @return TRUE=成功|FALSE=失败
	 * @param varchar(32) $userid
	 * @param varchar(32) $userpass
	 */
	static function _authLogin($userid,$userpass)
	{
		if(isset($userid) && isset($userpass))
		{
			$userpass = strlen($userpass) == 32?$userpass:md5($userpass);
			$userbase = Userbase::find('userid = ? and userpass = ?',$userid,$userpass)->getOne();
			if ($userbase->id())
			{
				if ($userbase->status == 0 )
				{
				$_SESSION[self::$_Login_SessionKey] = $userbase->toArray(); //赋予用户对象
				return true;
				}else {
					//锁定
					//$this->errmsg = '对不起,您的账号已经被锁定';
					return false;
				}
			}else
			{
				//$this->errmsg = '不存在的用户';
				return false;
			}
		}else
		{
			//$this->errmsg = '未设置账号密码';
			return false;
		}
	}
	/**
	 * 
	 *2011-12-31-下午12:09:40 by 460932465
	 * @access 检测是否登陆...
	 * @return BOOL,登陆=TRUE|未登陆=FALSE
	 */
	static function is_login()
	{
		
		return isset($_SESSION[self::$_Login_SessionKey]);
	}
	/**
	 * 
	 *2011-12-31-下午12:13:00 by 460932465
	 * @return 直接返回用户对象
	 */
	static function userbase()
	{
		
		return Userbase::find('userid = ? and userpass = ?',$_SESSION[self::$_Login_SessionKey]['userid'],$_SESSION[self::$_Login_SessionKey]['userpass'])->getOne();
	}
	/**
	 * 
	 *2011-12-31-下午12:35:08 by 460932465
	 * 用户退出
	 */
	static function outlogin()
	{
		if(isset($_SESSION[self::$_Login_SessionKey]))
		{
			unset($_SESSION[self::$_Login_SessionKey]);
		}
	}
	/**
	 * 
	 *2012-1-3-下午06:21:31 by 460932465
	 * @return 重置密码
	 * @param userbase $userbase
	 * @param string $newpass
	 */
	static function getpwd($userbase,$newpass)
	{
		$oldpass = strtolower(md5($userbase->userpass));
		$newpass = strtolower(md5($newpass));
		
		if ( $oldpass !== strtolower($userbase->userpass))
		{
			return false;
		}else if ($oldpass === strtolower($userbase->userpass))
		{
			//远程修改
			$change = new helper_interface_changepwd($userbase);
			$status = $change->change($oldpass,$newpass);
			if ($status === true)
			{
				QDB::getConn()->startTrans();
				try {
				$userbase->userpass = $newpass;
				$userbase->save(0,'update');
				QDB::getConn()->completeTrans(true);
				return true;
				} catch (Exception $e) {
				QDB::getConn()->completeTrans(false);
				return false;
				}
			}else
			{
				return false; //远程修改失败
			}
		}else
		{
			return false;
		}
		
	}
	static function changepwd($oldpass,$newpass)
	{
		$oldpass = strtolower(md5($oldpass));
		$newpass = strtolower(md5($newpass));
		$userbase = self::userbase();
		if ( $oldpass !== strtolower($userbase->userpass))
		{
			return false;
		}else if ($oldpass === strtolower($userbase->userpass))
		{
			//远程修改
			$change = new helper_interface_changepwd($userbase);
			$status = $change->change($oldpass,$newpass);
			if ($status === true)
			{
				QDB::getConn()->startTrans();
				try {
				$userbase->userpass = $newpass;
				$userbase->save(0,'update');
				QDB::getConn()->completeTrans(true);
				return true;
				} catch (Exception $e) {
				QDB::getConn()->completeTrans(false);
				return false;
				}
			}else
			{
				return false; //远程修改失败
			}
		}else
		{
			return false;
		}
	}
	static function get_userbase($where)
	{
		return Userbase::find($where)->getOne();
	}
}
?>